home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpsdi.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.3 KB  |  414 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpsdi.c,v 1.12.2.1 2000/11/09 23:24:18 rayjj Exp $ */
  20. /* Image compression for PostScript and PDF writers */
  21. #include "stdio_.h"        /* for jpeglib.h */
  22. #include "jpeglib_.h"        /* for sdct.h */
  23. #include "math_.h"
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gscspace.h"
  27. #include "gdevpsdf.h"
  28. #include "gdevpsds.h"
  29. #include "strimpl.h"
  30. #include "scfx.h"
  31. #include "sdct.h"
  32. #include "sjpeg.h"
  33. #include "slzwx.h"
  34. #include "spngpx.h"
  35. #include "srlx.h"
  36. #include "szlibx.h"
  37.  
  38. /* Define parameter-setting procedures. */
  39. extern stream_state_proc_put_params(s_CF_put_params, stream_CF_state);
  40.  
  41. /* ---------------- Image compression ---------------- */
  42.  
  43. /*
  44.  * Add a filter to expand or reduce the pixel width if needed.
  45.  * At least one of bpc_in and bpc_out is 8; the other is 1, 2, 4, or 8,
  46.  * except if bpc_out is 8, bpc_in may be 12.
  47.  */
  48. private int
  49. pixel_resize(psdf_binary_writer * pbw, int width, int num_components,
  50.          int bpc_in, int bpc_out)
  51. {
  52.     gs_memory_t *mem = pbw->dev->v_memory;
  53.     const stream_template *template;
  54.     stream_1248_state *st;
  55.     int code;
  56.  
  57.     if (bpc_out == bpc_in)
  58.     return 0;
  59.     if (bpc_in != 8) {
  60.     static const stream_template *const exts[13] = {
  61.         0, &s_1_8_template, &s_2_8_template, 0, &s_4_8_template,
  62.         0, 0, 0, 0, 0, 0, 0, &s_12_8_template
  63.     };
  64.  
  65.     template = exts[bpc_in];
  66.     } else {
  67.     static const stream_template *const rets[5] = {
  68.         0, &s_8_1_template, &s_8_2_template, 0, &s_8_4_template
  69.     };
  70.  
  71.     template = rets[bpc_out];
  72.     }
  73.     st = (stream_1248_state *)
  74.     s_alloc_state(mem, template->stype, "pixel_resize state");
  75.     if (st == 0)
  76.     return_error(gs_error_VMerror);
  77.     code = psdf_encode_binary(pbw, template, (stream_state *) st);
  78.     if (code < 0) {
  79.     gs_free_object(mem, st, "pixel_resize state");
  80.     return code;
  81.     }
  82.     s_1248_init(st, width, num_components);
  83.     return 0;
  84. }
  85.  
  86. /* Add the appropriate image compression filter, if any. */
  87. private int
  88. setup_image_compression(psdf_binary_writer *pbw, const psdf_image_params *pdip,
  89.             const gs_pixel_image_t * pim)
  90. {
  91.     gx_device_psdf *pdev = pbw->dev;
  92.     gs_memory_t *mem = pdev->v_memory;
  93.     const stream_template *template = pdip->filter_template;
  94.     const stream_template *orig_template = template;
  95.     const stream_template *lossless_template =
  96.     (pdev->params.UseFlateCompression &&
  97.      pdev->version >= psdf_version_ll3 ?
  98.      &s_zlibE_template : &s_LZWE_template);
  99.     const gs_color_space *pcs = pim->ColorSpace; /* null if mask */
  100.     int Colors = (pcs ? gs_color_space_num_components(pcs) : 1);
  101.     bool Indexed =
  102.     (pcs != 0 && 
  103.      gs_color_space_get_index(pcs) == gs_color_space_index_Indexed);
  104.     gs_c_param_list *dict = pdip->Dict;
  105.     stream_state *st;
  106.     int code;
  107.  
  108.     if (!pdip->Encode)        /* no compression */
  109.     return 0;
  110.     if (pdip->AutoFilter) {
  111.     /*
  112.      * Disregard the requested filter: use DCTEncode with ACSDict
  113.      * instead (or the lossless filter if the conditions for JPEG
  114.      * encoding aren't met).
  115.      *
  116.      * Even though this isn't obvious from the Adobe Tech Note,
  117.      * it appears that if UseFlateCompression is true, the default
  118.      * compressor for AutoFilter is FlateEncode, not LZWEncode.
  119.      */
  120.     orig_template = template = &s_DCTE_template;
  121.     dict = pdip->ACSDict;
  122.     }
  123.     if (template == 0)    /* no compression */
  124.     return 0;
  125.     if (pim->Width * pim->Height * Colors * pim->BitsPerComponent <= 160)    /* not worth compressing */
  126.     return 0;
  127.     /* Only use DCTE for 8-bit, non-Indexed data. */
  128.     if (template == &s_DCTE_template) {
  129.     if (Indexed ||
  130.         !(pdip->Downsample ?
  131.           pdip->Depth == 8 ||
  132.           (pdip->Depth == -1 && pim->BitsPerComponent == 8) :
  133.           pim->BitsPerComponent == 8)
  134.         ) {
  135.         /* Use LZW/Flate instead. */
  136.         template = lossless_template;
  137.     }
  138.     }
  139.     st = s_alloc_state(mem, template->stype, "setup_image_compression");
  140.     if (st == 0)
  141.     return_error(gs_error_VMerror);
  142.     if (template->set_defaults)
  143.     (*template->set_defaults) (st);
  144.     if (template == &s_CFE_template) {
  145.     stream_CFE_state *const ss = (stream_CFE_state *) st;
  146.  
  147.     if (pdip->Dict != 0 && pdip->filter_template == template) {
  148.         s_CF_put_params((gs_param_list *)pdip->Dict,
  149.                 (stream_CF_state *)ss); /* ignore errors */
  150.     } else {
  151.         ss->K = -1;
  152.         ss->BlackIs1 = true;
  153.     }
  154.     ss->Columns = pim->Width;
  155.     ss->Rows = (ss->EndOfBlock ? 0 : pim->Height);
  156.     } else if ((template == &s_LZWE_template ||
  157.         template == &s_zlibE_template) &&
  158.            pdev->version >= psdf_version_ll3) {
  159.     /* If not Indexed, add a PNGPredictor filter. */
  160.     if (!Indexed) {
  161.         code = psdf_encode_binary(pbw, template, st);
  162.         if (code < 0)
  163.         goto fail;
  164.         template = &s_PNGPE_template;
  165.         st = s_alloc_state(mem, template->stype, "setup_image_compression");
  166.         if (st == 0) {
  167.         code = gs_note_error(gs_error_VMerror);
  168.         goto fail;
  169.         }
  170.         if (template->set_defaults)
  171.         (*template->set_defaults) (st);
  172.         {
  173.         stream_PNGP_state *const ss = (stream_PNGP_state *) st;
  174.  
  175.         ss->Colors = Colors;
  176.         ss->Columns = pim->Width;
  177.         }
  178.     }
  179.     } else if (template == &s_DCTE_template) {
  180.     code = psdf_DCT_filter((dict != 0 && orig_template == template ?
  181.                 (gs_param_list *)dict : NULL),
  182.                    st, pim->Width, pim->Height, Colors, pbw);
  183.     if (code < 0)
  184.         goto fail;
  185.     /* psdf_DCT_filter already did the psdf_encode_binary. */
  186.     return 0;
  187.     }
  188.     code = psdf_encode_binary(pbw, template, st);
  189.     if (code >= 0)
  190.     return 0;
  191.  fail:
  192.     gs_free_object(mem, st, "setup_image_compression");
  193.     return code;
  194. }
  195.  
  196. /* Determine whether an image should be downsampled. */
  197. private bool
  198. do_downsample(const psdf_image_params *pdip, const gs_pixel_image_t *pim,
  199.           floatp resolution)
  200. {
  201.     floatp factor = (int)(resolution / pdip->Resolution);
  202.  
  203.     return (pdip->Downsample && factor >= pdip->DownsampleThreshold &&
  204.         factor <= pim->Width && factor <= pim->Height);
  205. }
  206.  
  207. /* Add downsampling, antialiasing, and compression filters. */
  208. /* Uses AntiAlias, Depth, DownsampleThreshold, DownsampleType, Resolution. */
  209. /* Assumes do_downsampling() is true. */
  210. private int
  211. setup_downsampling(psdf_binary_writer * pbw, const psdf_image_params * pdip,
  212.            gs_pixel_image_t * pim, floatp resolution)
  213. {
  214.     gx_device_psdf *pdev = pbw->dev;
  215.     /* Note: Bicubic is currently interpreted as Average. */
  216.     const stream_template *template =
  217.     (pdip->DownsampleType == ds_Subsample ?
  218.      &s_Subsample_template : &s_Average_template);
  219.     int factor = (int)(resolution / pdip->Resolution);
  220.     int orig_bpc = pim->BitsPerComponent;
  221.     int orig_width = pim->Width;
  222.     int orig_height = pim->Height;
  223.     stream_state *st;
  224.     int code;
  225.  
  226.     st = s_alloc_state(pdev->v_memory, template->stype,
  227.                "setup_downsampling");
  228.     if (st == 0)
  229.     return_error(gs_error_VMerror);
  230.     if (template->set_defaults)
  231.     template->set_defaults(st);
  232.     {
  233.     stream_Downsample_state *const ss = (stream_Downsample_state *) st;
  234.  
  235.     ss->Colors =
  236.         (pim->ColorSpace == 0 ? 1 /*mask*/ :
  237.          gs_color_space_num_components(pim->ColorSpace));
  238.     ss->WidthIn = pim->Width;
  239.     ss->HeightIn = pim->Height;
  240.     ss->XFactor = ss->YFactor = factor;
  241.     ss->AntiAlias = pdip->AntiAlias;
  242.     ss->padX = ss->padY = false; /* should be true */
  243.     if (template->init)
  244.         template->init(st);
  245.     pim->Width = s_Downsample_size_out(pim->Width, factor, ss->padX);
  246.     pim->Height = s_Downsample_size_out(pim->Height, factor, ss->padY);
  247.     pim->BitsPerComponent = pdip->Depth;
  248.     gs_matrix_scale(&pim->ImageMatrix, (double)pim->Width / orig_width,
  249.             (double)pim->Height / orig_height,
  250.             &pim->ImageMatrix);
  251.     /****** NO ANTI-ALIASING YET ******/
  252.     if ((code = setup_image_compression(pbw, pdip, pim)) < 0 ||
  253.         (code = pixel_resize(pbw, pim->Width, ss->Colors,
  254.                  8, pdip->Depth)) < 0 ||
  255.         (code = psdf_encode_binary(pbw, template, st)) < 0 ||
  256.         (code = pixel_resize(pbw, orig_width, ss->Colors,
  257.                  orig_bpc, 8)) < 0
  258.         ) {
  259.         gs_free_object(pdev->v_memory, st, "setup_image_compression");
  260.         return code;
  261.     }
  262.     }
  263.     return 0;
  264. }
  265.  
  266. /* Set up compression and downsampling filters for an image. */
  267. /* Note that this may modify the image parameters. */
  268. int
  269. psdf_setup_image_filters(gx_device_psdf * pdev, psdf_binary_writer * pbw,
  270.              gs_pixel_image_t * pim, const gs_matrix * pctm,
  271.              const gs_imager_state * pis)
  272. {
  273.     /*
  274.      * The following algorithms are per Adobe Tech Note # 5151,
  275.      * "Acrobat Distiller Parameters", revised 16 September 1996
  276.      * for Acrobat(TM) Distiller(TM) 3.0.
  277.      *
  278.      * The control structure is a little tricky, because filter
  279.      * pipelines must be constructed back-to-front.
  280.      */
  281.     int code = 0;
  282.     psdf_image_params params;
  283.     int bpc = pim->BitsPerComponent;
  284.     int bpc_out = pim->BitsPerComponent = min(bpc, 8);
  285.     int ncomp;
  286.     double resolution;
  287.  
  288.     /*
  289.      * The Adobe documentation doesn't say this, but mask images are
  290.      * compressed on the same basis as 1-bit-deep monochrome images,
  291.      * except that anti-aliasing (resolution/depth tradeoff) is not
  292.      * allowed.
  293.      */
  294.     if (pim->ColorSpace == NULL) { /* mask image */
  295.     params = pdev->params.MonoImage;
  296.     params.Depth = 1;
  297.     ncomp = 1;
  298.     } else {
  299.     ncomp = gs_color_space_num_components(pim->ColorSpace);
  300.     if (ncomp == 1) {
  301.         if (bpc == 1)
  302.         params = pdev->params.MonoImage;
  303.         else
  304.         params = pdev->params.GrayImage;
  305.         if (params.Depth == -1)
  306.         params.Depth = bpc;
  307.     } else {
  308.         params = pdev->params.ColorImage;
  309.         /* params.Depth is reset below */
  310.     }
  311.     }
  312.  
  313.     /*
  314.      * We can compute the image resolution by:
  315.      *    W / (W * ImageMatrix^-1 * CTM / HWResolution).
  316.      * We can replace W by 1 to simplify the computation.
  317.      */
  318.     if (pctm == 0)
  319.     resolution = -1;
  320.     else {
  321.     gs_point pt;
  322.  
  323.     /* We could do both X and Y, but why bother? */
  324.     gs_distance_transform_inverse(1.0, 0.0, &pim->ImageMatrix, &pt);
  325.     gs_distance_transform(pt.x, pt.y, pctm, &pt);
  326.     resolution = 1.0 / hypot(pt.x / pdev->HWResolution[0],
  327.                  pt.y / pdev->HWResolution[1]);
  328.     }
  329.     if (ncomp == 1) {
  330.     /* Monochrome, gray, or mask */
  331.     /* Check for downsampling. */
  332.     if (do_downsample(¶ms, pim, resolution)) {
  333.         /* Use the downsampled depth, not the original data depth. */
  334.         if (params.Depth == 1) {
  335.         params.Filter = pdev->params.MonoImage.Filter;
  336.         params.filter_template = pdev->params.MonoImage.filter_template;
  337.         params.Dict = pdev->params.MonoImage.Dict;
  338.         } else {
  339.         params.Filter = pdev->params.GrayImage.Filter;
  340.         params.filter_template = pdev->params.GrayImage.filter_template;
  341.         params.Dict = pdev->params.GrayImage.Dict;
  342.         }
  343.         code = setup_downsampling(pbw, ¶ms, pim, resolution);
  344.     } else {
  345.         code = setup_image_compression(pbw, ¶ms, pim);
  346.     }
  347.     if (code < 0)
  348.         return code;
  349.     code = pixel_resize(pbw, pim->Width, ncomp, bpc, bpc_out);
  350.     } else {
  351.     /* Color */
  352.     bool cmyk_to_rgb =
  353.         pdev->params.ConvertCMYKImagesToRGB &&
  354.         pis != 0 &&
  355.         gs_color_space_get_index(pim->ColorSpace) ==
  356.         gs_color_space_index_DeviceCMYK;
  357.  
  358.     if (cmyk_to_rgb)
  359.         pim->ColorSpace = gs_cspace_DeviceRGB(pis);
  360.     if (params.Depth == -1)
  361.         params.Depth = (cmyk_to_rgb ? 8 : bpc_out);
  362.     if (do_downsample(¶ms, pim, resolution)) {
  363.         code = setup_downsampling(pbw, ¶ms, pim, resolution);
  364.     } else {
  365.         code = setup_image_compression(pbw, ¶ms, pim);
  366.     }
  367.     if (cmyk_to_rgb) {
  368.         gs_memory_t *mem = pdev->v_memory;
  369.         stream_C2R_state *ss = (stream_C2R_state *)
  370.         s_alloc_state(mem, s_C2R_template.stype, "C2R state");
  371.         int code = pixel_resize(pbw, pim->Width, 3, 8, bpc_out);
  372.             
  373.         if (code < 0 ||
  374.         (code = psdf_encode_binary(pbw, &s_C2R_template,
  375.                        (stream_state *) ss)) < 0 ||
  376.         (code = pixel_resize(pbw, pim->Width, 4, bpc, 8)) < 0
  377.         )
  378.         return code;
  379.         s_C2R_init(ss, pis);
  380.     } else {
  381.         code = pixel_resize(pbw, pim->Width, ncomp, bpc, bpc_out);
  382.         if (code < 0)
  383.         return code;
  384.     }
  385.     }
  386.     return code;
  387. }
  388.  
  389. /* Set up compression filters for a lossless image, with no downsampling, */
  390. /* no color space conversion, and only lossless filters. */
  391. /* Note that this may modify the image parameters. */
  392. int
  393. psdf_setup_lossless_filters(gx_device_psdf *pdev, psdf_binary_writer *pbw,
  394.                 gs_pixel_image_t *pim)
  395. {
  396.     /*
  397.      * Set up a device with modified parameters for computing the image
  398.      * compression filters.  Don't allow downsampling or lossy compression.
  399.      */
  400.     gx_device_psdf ipdev;
  401.  
  402.     ipdev = *pdev;
  403.     ipdev.params.ColorImage.AutoFilter = false;
  404.     ipdev.params.ColorImage.Downsample = false;
  405.     ipdev.params.ColorImage.Filter = "FlateEncode";
  406.     ipdev.params.ColorImage.filter_template = &s_zlibE_template;
  407.     ipdev.params.ConvertCMYKImagesToRGB = false;
  408.     ipdev.params.GrayImage.AutoFilter = false;
  409.     ipdev.params.GrayImage.Downsample = false;
  410.     ipdev.params.GrayImage.Filter = "FlateEncode";
  411.     ipdev.params.GrayImage.filter_template = &s_zlibE_template;
  412.     return psdf_setup_image_filters(&ipdev, pbw, pim, NULL, NULL);
  413. }
  414.